﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using WgFrame.Net;

public class ZhaoMuResultPanel : BasePopupPanel
{
    private GList lsHeros;
    public override void Init()
    {
        base.Init();

        frame = view.GetChild("n1").asCom;
        lsHeros = frame.GetChild("n3").asList;
        lsHeros.onClickItem.Add(OnHeroInfo);
    }

    public override void Show(params object[] para)
    {
        base.Show(para);
        List<int> addHeros = (List<int>)para[0];
        SetInfo(addHeros);
    }

    public override void Close()
    {
        base.Close();
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_zhaomu_hero1").asCom;
    }

    private void SetInfo(List<int> addHeros)
    {
        lsHeros.RemoveChildrenToPool();
        for (int i = 0; i < addHeros.Count; i++)
        {
            if (InitHeroMgr.Dict.TryGetValue(addHeros[i], out InitHero initHero))
            {
                GButton button = lsHeros.GetFromPool("ui://f2iijc3nfz4ail").asButton;
                button.data = addHeros[i];

                GTextField title = button.GetChild("n1").asTextField;
                title.text = initHero.name;
                title.color = CommonUtil.GetHeroPinZhiColor(initHero.quality);
                Controller c1 = button.GetController("c1");
                c1.selectedIndex = initHero.quality - 1;
                lsHeros.AddChild(button);
            }
        }
    }

    private void OnHeroInfo(EventContext context)
    {
        if (lsHeros.selectedIndex < 0)
        {
            return;
        }

        GButton button = lsHeros.GetChildAt(lsHeros.selectedIndex).asButton;
        int id = (int)button.data;
        if (InitHeroMgr.Dict.TryGetValue(id, out InitHero initHero))
        {
            PopupPanelManager.Open<HeroInfoPanel>(new Hero(id));
        }
    }
}
